package gui.components.JXTable.JXTableModel;

import javax.swing.table.DefaultTableModel;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: Andry
 * Date: 02.11.2005
 * Time: 19:09:13
 * To change this template use File | Settings | File Templates.
 */
public class JXTableModel extends DefaultTableModel implements iJXTableModel{
    private boolean tBlock = false;
    private HashMap<Integer,Class> columnClass = new HashMap<Integer, Class>();
    private HashMap<Integer, ArrayList<Integer>> notEditableCell = new HashMap<Integer, ArrayList<Integer>>();
    private ArrayList<Integer> notEditableRows = new ArrayList<Integer>();
    private ArrayList<Integer> notEditableColumns = new ArrayList<Integer>();
    private ArrayList<Integer> remmovedColumns = new ArrayList<Integer>();
    public void removeColumn(Integer column){
        Vector vec = getColumnIdentifiers();
        vec.remove(column.intValue());
        setColumnIdentifiers(vec);
        if(columnClass.containsKey(column)){columnClass.remove(column.intValue());}
        if(notEditableColumns.contains(column)){notEditableColumns.remove(column);}
        remmovedColumns.add(column);
    }
    public void setColumnClass(Integer column, Class c){
        //if(columnClass.containsKey(column)){columnClass.remove(column);}
        columnClass.put(column,c);
    }
    public void addNotEditableRow(Integer row){
        notEditableRows.add(row);
    }
    public void removeNotEditableRow(Integer row){
        boolean b = notEditableRows.remove(row);
    }
    public void addNotEditableColumn(Integer column){
        notEditableColumns.add(column);
    }
    public void removeNotEditableColumn(Integer column){
        boolean b = notEditableColumns.remove(column);
    }
    public void addNotEditableCell(Integer row, Integer column){
        if(!notEditableCell.containsKey(row)){
            ArrayList<Integer> tmp = new ArrayList<Integer>();
            tmp.add(column);
            notEditableCell.put(row, tmp);
        }else{
            notEditableCell.get(row).add(column);
        }
    }
    public void removeNotEditableCell(Integer row, Integer column){
        boolean b = notEditableCell.get(row).remove(column);
    }
    public void tableBlock(boolean b){
        tBlock = b;
    }
    public boolean isCellEditable(int row, int column){
        if(notEditableRows.contains(row))return false;
        if(notEditableColumns.contains(column))return false;
        if (notEditableCell.containsKey(row)){
            if(notEditableCell.get(row).contains(column)){
                return false;
            }
        }
        return true;
        //return !tBlock;
    }
    public void setValueAt(Object aValue, int row, int column){
        if(column>0){super.setValueAt(aValue, row, column);}
    }

    /*public Object getValueAt(int row, int column){
        if(remmovedColumns.contains(column))return "";
        return super.getValueAt(row, column);
    }*/

    public Vector getColumnIdentifiers(){
        return columnIdentifiers;
    }
    public Class getColumnClass(int column) {
        if (!tBlock){
            if(columnClass.containsKey(column)){
                return columnClass.get(column);
            }
        }
        return Object.class;
    }
}
